package com.itkaisa.wallet.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itkaisa.wallet.mapper.AccountMapper;
import com.itkaisa.wallet.pojo.Account;
import com.itkaisa.wallet.service.AccountService;
import com.itkaisa.wallet.service.TransactionDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author HP
 * @version 1.0
 * @description TODO
 * @date 2023/3/16 3:07
 */
@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper,Account> implements AccountService {

    @Autowired
    private AccountMapper accountMapper;

    @Autowired
    private TransactionDetailsService transactionDetailsService;

    @Override
    public Double selectByBalance(int userId) {
        return accountMapper.selectByBalance(userId);
    }

    @Override
    public boolean cost100(int userId, Integer amount) {
        Double balance = selectByBalance(userId);
        if (balance == null || balance < amount){
            return false;
        }else {
            accountMapper.cost100(userId,amount);
            transactionDetailsService.costType(userId,amount,"COST");
        }
        return true;
    }

    @Override
    public void refund20(int userId, Integer amount) {
        accountMapper.refund20(userId,amount);
        transactionDetailsService.inComeType(userId,amount,"INCOME");
    }



}
